package com.example.gybicycle.controller.web;

import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.redis.RedisUtil;
import com.example.gybicycle.common.util.DrawImage;
import com.example.gybicycle.common.util.LoginUtil;
import com.example.gybicycle.entity.Code;
import com.example.gybicycle.entity.LoginInfo;
import com.example.gybicycle.entity.SysUser;
import com.example.gybicycle.entity.UserUpPwd;
import com.example.gybicycle.service.SysUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@Tag(name = "登录")
@RestController
public class LoginController {
    @Autowired
    private SysUserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @PostMapping("/login")
    @Operation(summary = "登录")
    @ApiOperationSupport(includeParameters = {"sysUser.name","sysUser.password"})
    public Result login(@RequestBody SysUser sysUser,HttpServletRequest request){
        Result result;
        result =userService.login(sysUser, request);
        return result;
    }
    @GetMapping("/getImage")
    @Operation(summary = "验证码")
    public Result getImage(HttpServletRequest request){
        DrawImage drawImage = new DrawImage();
        String code=drawImage.getrandomString();
        String str= drawImage.getImage(request,code);
        LoginInfo info=LoginUtil.getLoginInfo(request);
        if(info==null){
            info=new LoginInfo();
        }
        SysUser user=new SysUser();
        user.setCode(code);
        info.setUser(user);
        info.setAuthorization(UUID.randomUUID().toString()+"-yyyy-MM-dd");
        Code code1=new Code();
        code1.setAuthorization(info.getAuthorization());
        code1.setCode(str);
        LoginUtil.setLoginInfo(info);
        Result result=Result.success(code1);
        System.out.println("login-code:"+info.getAuthorization());
        return result;
    }
    @PostMapping("/userInfo")
    @Operation(summary ="登录用户信息")
    public Result userInfo(HttpServletRequest request){
        LoginInfo info=LoginUtil.getLoginInfo(request);
        return Result.success(info);
    }
    @PostMapping("/getMenu")
    @Operation(summary ="获取菜单")
    public Result getMenu(HttpServletRequest request){

        LoginInfo info= LoginUtil.getLoginInfo(request);
        if(info==null)
            return Result.loginOut();
        info=userService.getMenu(info);
        LoginUtil.setLoginInfo(info);
        return Result.success(info.getTreeMenuList());
    }

    @PostMapping("/loginout")
    @Operation(summary ="退出登录")
//    @SysLoging("登录管理-退出登录")
    public Result login( HttpServletRequest request){
        LoginUtil.loginout(request);
        return Result.success("退出登录成功");
    }
    @PostMapping("/uppwd")
    @Operation(summary ="修改密码")
    public Result uppwd(@RequestBody UserUpPwd pwd, HttpServletRequest request){
        LoginInfo info=LoginUtil.getLoginInfo(request);
        if(info==null)
            return Result.loginOut();
        SysUser user=new SysUser();
        user.setId(info.getUser().getId());
        user.setPassword(pwd.getOldpassword());
        List<SysUser> list=userService.findList(user);
        if(list.size()==1){
            user.setPassword(pwd.getPassword());
            int num=userService.update(user);
            if(num>0)
                return Result.success("修改密码成功");
        }else return Result.fail("原密码错误");
        return Result.success("修改密码失败");
    }


}
